//
//  MianPageViewController.m
//  A段项目  穷游
//
//  Created by 韩旭 on 16/6/17.
//  Copyright © 2016年 基础. All rights reserved.
//

#import "MianPageViewController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "HX_Macro.h"
@interface MianPageViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageViewPic;
@property (nonatomic, retain) UIPageControl *pageCon;
@property (nonatomic, retain) UIButton *btn;
@end

@implementation MianPageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    _picArr = @[
                [UIImage imageNamed:@"g1"],
                [UIImage imageNamed:@"g2"],
                [UIImage imageNamed:@"g3"]
                ].mutableCopy;
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem =  leftBtnItem;
    [leftBtn setImage:[UIImage imageNamed:@"xiangzuojian"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(addBackBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self creatScrollerView];
    [self creatPageControl];
}
- (void)addBackBtn:(UIButton *)btn {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
+ (instancetype)sharedWithVc {
    static MianPageViewController *guidePageVC = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        guidePageVC = [[MianPageViewController alloc]init];
    });
    return guidePageVC;
}
-(void)creatScrollerView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.scrollView.delegate = self;
    //Delegate
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(4 * SCREEN_WIDTH, SCREEN_HEIGHT);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //创建 imageView
    for (int i = 1; i <= self.picArr.count; i++) {
        self.imageViewPic = [[UIImageView alloc] initWithFrame:CGRectMake(WIDETH * (i - 1), 0, WIDETH, HEIGHT)];
        
        self.imageViewPic.image = self.picArr[i - 1];
        self.imageViewPic.tag = 100 + i;//tag
        [self.scrollView addSubview:self.imageViewPic];
        if (i==3) {
            UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(WIDETH / 3.5, HEIGHT * 0.85, WIDETH * 0.4, 50)];
            btn.backgroundColor = [UIColor clearColor];
            [btn setTitle:@"立即进入" forState:UIControlStateNormal];
            self.imageViewPic.userInteractionEnabled = YES;
            [self.imageViewPic addSubview:btn];
            [btn addTarget:self action:@selector(btnChlik:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}
-(void)creatPageControl{
    self.pageCon = [[UIPageControl alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 40)/2,  SCREEN_HEIGHT - 50, 40, 20)];
    self.pageCon.numberOfPages = 3;
    self.pageCon.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageCon.pageIndicatorTintColor = [UIColor grayColor];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageCon.currentPage = self.scrollView.contentOffset.x/WIDETH;
}
- (void)btnChlik:(UIButton *)btn {
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    MainViewController *view = [[MainViewController alloc]init];
    app.window.rootViewController = view;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
